package com.raquel.onceuponatime;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Class for Update a selected tale from database
 * Servlet implementation class Update
 * @author raquel_valenzuela
 */
@WebServlet("/Update")
public class Update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Update() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Tale tale = selectTale(request.getParameter("id"));
		request.setAttribute("tale", tale);
		
		//Redirecting to update.jsp
		this.getServletContext().getRequestDispatcher("/update.jsp").forward(request, response);
	}
	/**
	 * Gets a tale from database and returns a Tale
	 * @param id
	 * @return Tale
	 */
	private Tale selectTale(String id) {
		// TODO Auto-generated method stub
		Tale tale= new Tale();
		String urlcon="jdbc:mysql://localhost:3306/onceupon";
		ResultSet resultSet=null;
		
		try{
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = (Connection)DriverManager.getConnection(urlcon,"root","");
		
		Statement stat= (Statement) conn.createStatement();
		resultSet= stat.executeQuery("select * from tale where id="+id);
		
		if(resultSet.next()){
			tale.setId(resultSet.getInt("id"));
			tale.setTale(resultSet.getString(4));
			tale.setTitle(resultSet.getString("title"));
		}
		
		resultSet.close();
		conn.close();
		
		}catch (SQLException e){
			e.printStackTrace();
			
		}catch(ClassNotFoundException e){
			e.printStackTrace();
			
		}
		
		return tale;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		updateTale(request.getParameter("id"),request.getParameter("title"),request.getParameter("tale"));
		
		this.getServletContext().getRequestDispatcher("/Home").forward(request, response);
	}
	/**
	 * 
	 * @param id
	 * @param title
	 * @param tale
	 */
	private void updateTale(String id, String title,String tale) {
		String urlcon="jdbc:mysql://localhost:3306/onceupon";
		ResultSet resultSet=null;
		try{
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = (Connection)DriverManager.getConnection(urlcon,"root","");
		String updateSQL="update tale set title="+title+", tale="+tale+" where id="+id;
		PreparedStatement prep= conn.prepareStatement(updateSQL);
		
		prep.executeUpdate();
		prep.close();
		conn.close();
		}catch (SQLException e){
			e.printStackTrace();
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}
	}

}
